<template>
  <!-- 弹框 -->
  <el-dialog :visible.sync="dialogShow" :title="title" @close="onClose">
    <!-- 表单区域 -->
    <el-form
      ref="form"
      label-width="140px"
      :model="formData"
      :rules="formRules"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="formData.username"
          type="textarea"
          :rows="3"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="formData.mobile"
          type="textarea"
          :rows="3"
          placeholder="请输入"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="formData.password" />
      </el-form-item>
      <el-table-column prop="sex" label="性别">
        <el-switch
          v-model="value"
          active-text="男"
          inactive-text="女"
        />
      </el-table-column>
      <el-form-item label="">
        <el-upload
          :file-list="fileList"
          action="#"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
        </el-upload>
      </el-form-item>
      <el-row type="flex" justify="center">
        <el-button
          type="primary"
          size="small"
          @click="onSubmit"
        >提交</el-button>
        <el-button size="small" @click="dialogShow = false">关闭</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>
<script>
import { addyddUserInfoApi, updateyddUserInfoApi } from '@/api/ydd_user'
// import { UploadFile } from '@/api/user'
export default {
  name: 'YddUser',
  data() {
    return {
      value: true, // 开关按钮
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      formData: {
        username: '',
        mobile: '',
        password: '',
        sex: '',
        icon: ''
      },
      fileList: [], // 回显 {url,name}
      // 表单验证规则
      formRules: {
        username: [{ required: true, message: '请输入', trigger: 'blur' }],
        advpomobilesname: [{ required: true, message: '请输入', trigger: 'blur' }],
        password: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  methods: {
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      this.formData.id
        ? await updateyddUserInfoApi(this.formData)
        : await addyddUserInfoApi(this.formData)
      // 提示效果
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      // 关闭弹框
      this.dialogShow = false
      // 列表更新
      this.$emit('updateList')
    },
    // 弹框关闭
    onClose() {
      // 表单重置
      this.formData = {
        username: '',
        mobile: '',
        password: '',
        sex: '',
        icon: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
      // 清除回显
      this.fileList = []
    }

  }
}
</script>
<style scoped></style>
